//
//  BGFaBuZNXController.m
//  bigMall
//
//  Created by ios on 16/8/31.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGFaBuZNXController.h"
#import "BGRenZhengoneCell.h"
#import "BGDateResult.h"
#import "BGOwnMessageListResultModel.h"

@interface BGFaBuZNXController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate> {
    
    UIBarButtonItem *right;
    UITableView *_tableView;
    NSArray *arr;
    NSString *shoujianren;
    NSString *zhuti;
    NSString *neirong;
    
}

@end

@implementation BGFaBuZNXController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发送站内信";
    self.view.backgroundColor = [UIColor whiteColor];
    
   right = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(send)];
    self.navigationItem.rightBarButtonItem = right;
    // Do any additional setup after loading the view.
    
    arr = [NSArray arrayWithObjects:@"收件人：",@"主题：", nil];
    
    [self createUi];
    
    shoujianren = self.name;
    
}
- (void)send {
 
    if (shoujianren == nil || zhuti == nil || neirong == nil|| [shoujianren isEqual:@""]|| [zhuti isEqual:@""] || [neirong isEqual:@""]) {
        [self showMessage:@"信息不全,请完善"];
        return;
    }
    NSString *type;
    if ([self isMobile:shoujianren]) {
        type = @"phone ";
    }else{
        type = @"name";
    }

    [BGDateResult sendMessageWithTo:shoujianren toType:type title:zhuti cont:neirong success:^(NSObject *result) {
        
        BGsendMessageModel *model = [[BGsendMessageModel alloc] init];
        model = (BGsendMessageModel*)result;
        
        if ([model.status isEqualToString:@"1"]) {
            [self showMessage:model.msg];
        } else {
            
            [self showMessage:model.msg];
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
/**
 *  手机号码验证
 *
 *  @param mobileNumbel 传入的手机号码
 *
 *  @return 格式正确返回true  错误 返回fals
 */
- (BOOL) isMobile:(NSString *)mobileNumbel{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,181(增加)
     */
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNumbel]
         || [regextestcm evaluateWithObject:mobileNumbel]
         || [regextestct evaluateWithObject:mobileNumbel]
         || [regextestcu evaluateWithObject:mobileNumbel])) {
        return YES;
    }
    
    return NO;
}

- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)createUi {
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _tableView.allowsSelection = NO;
    
    [self.view addSubview:_tableView];
    
}

#pragma mark - delegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0 || indexPath.row == 1) {
    
        return 61;
        
    } else {
        
        return 90;
        
    }
    
}
- (void)setName:(NSString *)name
{
    _name = name;
    shoujianren = name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.row == 0 || indexPath.row == 1) {
        
        [tableView registerNib:[UINib nibWithNibName:@"BGRenZhengoneCell" bundle:nil] forCellReuseIdentifier:@"celId"];
        BGRenZhengoneCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
        cel.title.text = arr[indexPath.row];
        cel.mesage.placeholder = @"请输入";
        if (shoujianren && indexPath.row == 0) {
            cel.mesage.text = shoujianren;
            cel.userInteractionEnabled = NO;
        }else{
            cel.userInteractionEnabled = YES;
        }
        cel.mesage.tag = indexPath.row + 1;
        [cel.mesage addTarget:self action:@selector(textFiled:) forControlEvents:UIControlEventEditingChanged];
        return cel;
        
    } else {
        
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cel"];
        
        UITableViewCell *cel = [[UITableViewCell alloc] init];
//        
        UILabel *textLab = [[UILabel alloc] initWithFrame:CGRectMake(17, 19, 89, 21)];
        textLab.font = [UIFont systemFontOfSize:16];
        textLab.text = @"内容：";
        [cel addSubview:textLab];
//
        UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(107, 12, BGWidth - textLab.right - 34 - 5, 70)];
        textview.text = @"输入详细的内容";
        textview.textColor = lightGray_;
        textview.font = [UIFont systemFontOfSize:16];
        textview.delegate = self;
        
        [cel addSubview:textview];
        
        return cel;
        
    }
    
}
//- (void)textViewDidEndEditing:(UITextView *)textView {
//    
//    neirong = textView.text;
//    
//}
- (void)textViewDidChange:(UITextView *)textView {
    neirong = textView.text;
    
}


- (void)textFiled:(UITextField *)textFiled {
    
    if (textFiled.tag == 1) {
        shoujianren = textFiled.text;
    } else if (textFiled.tag == 2) {
        zhuti = textFiled.text;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
+ (BGFaBuZNXController *)share {
    
    static BGFaBuZNXController *a = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate,^{
        a = [[self alloc] init];
    });
    return a;
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
